import {
  wxRequest
} from '../../../utils/server.js';


const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    orgTypeIndex: 0,
    orgType: '',
    orgTypeList: [],
    bizOrgItem:{}
  },

  onLoad: function (options) {
    var wxitem = wx.getStorageSync('staffItem')
    wx.setNavigationBarTitle({
      title: '发布' + app.globalData.memeberText,
    })
    if (wxitem) {
      this.setData({
        cententDetail: wxitem,
        bizOrgItem: {
          orgId: wxitem.orgId,
          orgName: wxitem.orgName
          }
      })
      console.log('bizOrgItem:', wxitem)
    }

    this.getOrgType()
  },

  onUnload: function () {
    wx.removeStorageSync('staffItem')
  },

  onHide: function () {
    wx.removeStorageSync('staffItem')
  },

  onShow: function() {
   
  },

  //选择组织类型
  pickerChange: function(e) {
    var index = e.detail.value;
    var orgTypeList = this.data.orgTypeList;
    var orgType = ''
    orgTypeList.map((item, i) => {
      if (i == index) {
        orgType = item.key
      }
    })

    this.setData({
      orgTypeIndex: index,
      orgType,
      bizOrgItem:{}
    })
  },

  // 组织类型查询
  getOrgType: function() {
    var that=this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findOrgType',
      method: 'post',
      data:{}
    }, (res) => {

      var list = res.list
      var orgTypeIndex=''
      var orgType=''


      // 默认只有一个身份
      if(list.length==1){
        that.setData({
          orgTypeIndex:0,
          orgType: list[0].key
          })
        that.getBizorgList()
      }
 

      //初始化编辑
      if (that.data.cententDetail && that.data.cententDetail.id){
        list.map((item, index) => {
          if (item.key == that.data.cententDetail.orgType) {
            orgTypeIndex = item.index,
            orgType=item.key
          }
        })
        that.setData({
          orgTypeIndex,
          orgType,
          // bizOrgItem: {
          //   orgId: that.data.cententDetail.orgId,
          //   orgName: that.data.cententDetail.orgName
          // }
        })
      }
      


      this.setData({       
        orgTypeList: list       
      })
    })
  },


  handleBizOrg: function() {
    wx.navigateTo({
      url: '/pages/publish/template/bizorg?orgType=' + this.data.orgType,
    })
  },


  //发布员工
  formSubmit: function(e) {
    var formdata = e.detail.value;
    formdata.orgType = this.data.orgType;

    if (this.data.bizOrgItem && this.data.bizOrgItem.orgId){
      formdata.orgId = this.data.bizOrgItem.orgId 
    }
    
    //编辑
    if (this.data.cententDetail && this.data.cententDetail.userType){
      formdata.userType = this.data.cententDetail.userType
    }

    if (formdata.orgType == '' || this.data.orgTypeList.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '组织类型为空',
      })
      return
    }
    console.log(this.data.orgTypeList)

    console.log(formdata.orgId,'formdata.orgId')
    if (!formdata.orgId) {
      wx.showToast({
        icon: 'none',
        title: '请选择商业组织',
      })
      return
    }


    if (formdata.realName == '') {
      wx.showToast({
        icon: 'none',
        title: '员工姓名不能为空',
      })
      return
    }

    if (formdata.mobile == '') {
      wx.showToast({
        icon: 'none',
        title: '手机号码不能为空',
      })
      return
    }

    if (formdata.mobile.length != 11 && formdata.mobile.length != 8 && formdata.mobile.length != 10) {
      wx.showToast({
        icon: 'none',
        title: '手机号码格式不正确',
      })
      return
    }

    if (formdata.mobile.length == '10' && formdata.mobile.slice(0, 2) != '09') {
      // 台湾手机号 09开头+任意8位数
      wx.showToast({
        icon: 'none',
        title: '台湾手机号码格式不正确',
      })
      return
    }


    var url = '/sys/protected/user/createEmployee';
    var title = "发布成功"
    //修改
    if (this.data.cententDetail && this.data.cententDetail.id) {
      url = "/sys/protected/user/updateEmployee";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log('发布结果：',res)

      wx.showToast({
        title
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/staff/list?orgType=' + this.data.orgType,
        })
      }, 1500)
    })
  },



  goback: function() {
    wx.navigateBack({

    })
  },



  // 获取列表
  getBizorgList: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findBizOrg',
      method: 'POST',
      data: {
        orgType: that.data.orgType
      }
    }, (res) => {
      var list=res.list
      if(list.length==1){
        that.setData({
          bizOrgItem: {
            orgId: list[0].orgId,
            orgName: list[0].orgName
          }
        })
      }
    
    })


  },


})